import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/home/news_comment_entity.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/module/expert/items/expert_logo.dart';
import 'package:ball_lion/module/home/comments/comment_sheet.dart';
import 'package:ball_lion/widgets/asset_item.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';
import 'package:ball_lion/widgets/card.dart';
import 'package:ball_lion/widgets/circular.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/base/wqs_dialog.dart';
import 'package:ball_lion/widgets/select_bottomsheet.dart';

class CommentItem extends StatelessWidget {
  const CommentItem(
      {super.key,
      this.comment,
      this.onLike,
      this.onReply,
      this.onDelete,
      this.onReport,
      required this.newsId,
      this.isSecondary = false,
      this.isOrigin = false});

  final NewsCommentEntity? comment;
  final int newsId;
  final bool isSecondary;
  final bool isOrigin;
  final ValueChanged<int>? onLike;
  final Function()? onReply;
  final Function()? onDelete;
  final ValueChanged<String>? onReport;

  @override
  Widget build(BuildContext context) {
    return Block(
      onTap: () {
        if (isSecondary) {
          onReply?.call();
        } else {
          CommentSheet.show(comment, newsId, false);
        }
      },
      cardStyle: const CardStyle(
        padding: EdgeInsets.symmetric(vertical: 10),
        color: Colours.white,
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          GestureDetector(
            onTap: () => Get.toNamed(Routes.userPage,
                arguments: comment?.userId, parameters: {"type": "1"}),
            child: ExpertLogo(
              logo: comment?.userLogo,
              size: 32,
            ),
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: 32,
                  child: Row(
                    children: [
                      GestureDetector(
                        onTap: () => Get.toNamed(Routes.userPage,
                            arguments: comment?.userId,
                            parameters: {"type": "1"}),
                        child: QsText(comment?.userName ?? "",
                            fontSize: 16, fontWeight: FontWeight.w500),
                      ),
                      const Spacer(),
                      AssetItem(
                        padding: const EdgeInsets.only(left: 5),
                        direction: AssetDirection.ltr,
                        asset: comment?.isLike == 1 ? 'zan_red.png' : 'zan.png',
                        name: (comment?.likeNum.hasValue ?? false)
                            ? "${comment?.likeNum ?? 0}"
                            : null,
                        assetSize: 14,
                        space: 4,
                        textStyle: TextStyle(
                            fontSize: 13,
                            color: comment?.isLike == 1
                                ? Colours.main
                                : Colours.grey99),
                        onTap: () {
                          onLike?.call(comment?.isLike == 1 ? 0 : 1);
                        },
                      )
                    ],
                  ).paddingOnly(left: 8),
                ),
                if (comment?.fromComment.hasValue ?? false)
                  Circular(
                    width: Get.width,
                    color: Colours.greyF5F7FA,
                    radius: 4,
                    margin: const EdgeInsets.only(left: 4, top: 8),
                    padding: const EdgeInsets.all(8),
                    child: QsText(comment?.fromComment ?? "",
                        fontSize: 16, color: Colours.grey66),
                  ),
                if (comment?.special.hasValue ?? false)
                  QsText(comment?.special ?? "",
                      fontSize: 16, color: Colours.grey99)
                else
                  QsText(
                    comment?.comment ?? "",
                    fontSize: 16,
                    maxLines: 99,
                  ).paddingOnly(left: 8, top: 4, bottom: 10),
                Row(
                  children: [
                    GestureDetector(
                      onTap: () {
                        if (isSecondary) {
                          onReply?.call();
                        } else {
                          CommentSheet.show(comment, newsId, true);
                        }
                      },
                      child: Circular(
                        color: Colours.greyF2,
                        radius: 12,
                        padding: const EdgeInsets.symmetric(
                            horizontal: 8, vertical: 4),
                        child: AssetItem(
                          direction: AssetDirection.rtl,
                          asset: "arrow_right.png",
                          name:
                              "${comment?.commentNum.hasValue ?? false ? comment?.commentNum : ""}回复",
                          assetSize: 10,
                          textStyle: const TextStyle(
                              fontSize: 12, color: Colours.text),
                        ),
                      ),
                    ),
                    Container(width: 10),
                    QsText(
                      "${comment?.createTime ?? ""} · 来自${comment?.addr ?? ""}",
                      fontSize: 12,
                      color: Colours.grey99,
                    ),
                    const Spacer(),
                    if (!isOrigin)
                      if (comment?.isUser == 1)
                        GestureDetector(
                          onTap: () async {
                            final r = await Get.dialog(
                                    const QsDialog.alert("确认删除评论吗?")) ??
                                false;
                            if (r == true) onDelete?.call();
                          },
                          child: Container(
                              alignment: Alignment.centerRight,
                              width: 50,
                              height: 20,
                              child: const QsText("删除",
                                  fontSize: 12, color: Colours.grey99)),
                        )
                      else
                        GestureDetector(
                          onTap: () async {
                            final opts = [
                              "淫秽色情",
                              "营销广告",
                              "网络暴力",
                              "违法信息",
                              "虚假谣言"
                            ];
                            int? i =
                                await Get.bottomSheet(SelectBottomSheet(opts));
                            if (i != null) onReport?.call(opts[i]);
                          },
                          child: Container(
                            alignment: Alignment.centerRight,
                            width: 50,
                            height: 20,
                            child: Image.asset(
                                Utils.getImgPath("post_close.png"),
                                width: 10,
                                height: 10),
                          ),
                        )
                  ],
                ).paddingOnly(left: 8)
              ],
            ),
          )
        ],
      ),
    );
  }
}
